package cate.common.table.item.furniture.sheet;

import cate.common.table.ESheet;
import cate.common.table.item.furniture.row.FurnitureRandomAttrRow;
import cate.common.util.random.RWList;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class FurnitureRandomAttrSheet extends ESheet<FurnitureRandomAttrRow> {
    Map<Integer, RWList<FurnitureRandomAttrRow>> randoms = new HashMap<>();
    @Override
    public String getSheetName() {
        return "手办随机属性组";
    }

    @Override
    public void onLoaded() {
        randoms.clear();
        for (FurnitureRandomAttrRow row : getList()) {
            RWList<FurnitureRandomAttrRow> list = randoms.computeIfAbsent(row.group, e -> new RWList<>());
            list.add(row);
        }
        randoms.values().forEach(RWList::setup);
    }

    public FurnitureRandomAttrRow randomAttr(int group, byte limit) {
        RWList<FurnitureRandomAttrRow> list = randoms.get(group);
        if (list == null || list.isEmpty()) {
            return null;
        }
        if (limit > 0) {
            RWList<FurnitureRandomAttrRow> newList = new RWList<>();
            for (FurnitureRandomAttrRow row : list.list) {
                if (row.limit == 0 || row.limit == limit) {
                    newList.add(row);
                }
            }
            return newList.setup().get();
        }
        return list.get();
    }

    public FurnitureRandomAttrRow randomAttrExcludeOne(int group, byte limit, int excludeId) {
        RWList<FurnitureRandomAttrRow> list = randoms.get(group);
        if (list == null || list.isEmpty()) {
            return null;
        }
        RWList<FurnitureRandomAttrRow> newList = new RWList<>(
                list.list.stream().filter(e -> e.id != excludeId && (limit == 0 || limit == e.limit)).collect(Collectors.toList()));
        return newList.setup().get();
    }
}
